package config

import (
	"fmt"
	"os"
	"runtime"

	"gitee.com/freedom_feel/gui-bos-api/common/type"
	"gitee.com/freedom_feel/gui-bos-api/global"
	"gitee.com/freedom_feel/gui-bos-api/logs"

	"github.com/spf13/viper"
)

func ViperInit() {
	// 获取操作系统信息
	os1 := runtime.GOOS
	logs.Info(fmt.Sprintf("Current operating system is: %s", os1))
	fileNmae := ""
	// 判断操作系统类型
	switch os1 {
	case "linux":
		logs.Info("This is a Linux system.")
		fileNmae = "prodConfig"
	case "darwin":
		logs.Info("This is a macOS system.")
		fileNmae = "prodConfig"
	case "windows":
		logs.Info("This is a Windows system.")
		fileNmae = "devConfig"
	default:
		logs.Info(_type.StrPinJie("This is an unsupported operating system: ", os1))
		_type.ErrChan <- *_type.ErrSystem("配置文件读取失败")
		return
	}
	viper.SetConfigName(fileNmae)
	viper.SetConfigType("yaml")
	pwd, _ := os.Getwd()
	logs.Info(pwd)
	viper.AddConfigPath(pwd)
	if err := viper.ReadInConfig(); err != nil {
		logs.Error(fmt.Sprintf("read config file error: %v", err))
		_type.ErrChan <- *_type.ErrSystem("配置文件读取失败")
		return
	}
	logs.Info("Viper init success")
	if err := viper.Unmarshal(&global.Config); err != nil {
		logs.Error(fmt.Sprintf("read config file error: %v", err))
		_type.ErrChan <- *_type.ErrSystem("配置解析失败")
		return
	}
}
